package wikibooks;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;

/**
 * Static message log used by all classes. After running the converter, write
 * messages to "log.txt" with <code>Log.write();</code> or to another file
 * with <code>Log.write(File file)</code>, then clear it with
 * <code>Log.clear()</code>. Alternatively, get standard messages with
 * <code>String[] messages = Log.messages();</code>, warning messages with
 * <code>Log.warnings()</code>, and error messages with
 * <code>Log.errors()</code>. Remember to clear it if necessary.
 */
class Log {

    private static ArrayList<String> messages = new ArrayList<String>();
    private static ArrayList<String> warnings = new ArrayList<String>();
    private static ArrayList<String> errors = new ArrayList<String>();

    public static void addMessage(String message) {
        messages.add(message);
    }

    public static void addWarning(String warning) {
        warnings.add(warning);
    }

    public static void addError(String error) {
        errors.add(error);
    }

    public static String[] messages() {
        return messages.toArray(new String[]{});
    }

    public static String[] warnings() {
        return warnings.toArray(new String[]{});
    }

    public static String[] errors() {
        return errors.toArray(new String[]{});
    }

    public static void write() {
        write(new File("log.txt"));
    }

    public static void write(File file) {
        try {
            PrintStream log = new PrintStream(file);
            log.print("Wiki2LaTeX log: " + messages.size() + " standard message(s), " + warnings.size() + " warning(s), and " + errors.size() + " error(s).\n\n");
            for (String message : messages()) {
                log.print("---------------------------\n" + message + "\n---------------------------\n\n");
            }
            for (String warning : warnings()) {
                log.print("---------------------------\n" + warning + "\n---------------------------\n\n");
            }
            for (String error : errors()) {
                log.print("---------------------------\n" + error + "\n---------------------------\n\n");
            }
        } catch (FileNotFoundException e) {
        }
    }

    public static void clear() {
        messages.clear();
        warnings.clear();
        errors.clear();
    }
}
